package starlingui.state.desktopState
{
	import com.imcrl.core.imcrl_internal;
	import com.imcrl.event.ListItemEvent;
	
	import flash.geom.Point;
	
	import starling.events.Event;
	
	import starlingui.component.AdvLayout;
	import starlingui.component.AdvPageList;
	import starlingui.component.DeskList;
	import starlingui.component.Desktop;
	import starlingui.component.FolderList;
	import starlingui.component.itemRender.IconItemRender;
	
	use namespace imcrl_internal;
	public class FolderState extends DefaultState implements IFolderAction
	{
		public static const NAME:String="FolderState";
		
		private var deskList:DeskList;
		private var layout:AdvLayout;
		private var folderList:FolderList;
		private var openApplicationHandle:Function;
		public function FolderState(desktop:Desktop)
		{
			super(desktop);
			_name=NAME;
		}
		
		override public function start():void{
			deskList=desktop.current as DeskList;
			deskList.touchable=false;
			folderList=desktop.folderList;
			openApplicationHandle=desktop.openApplicationHandle;
			if(openApplicationHandle !=null ){
				folderList.addEventListener(ListItemEvent.SELECTED,selectAppHandle);
				folderList.addEventListener(ListItemEvent.SELECTED_SHAKE,shakeHandle);
			}
			
			layout=deskList.layout;
			desktop.scrollEnabeld=false;
			desktop._cache_top.addEventListener(Event.TRIGGERED,hideHandle);
			desktop._cache_botton.addEventListener(Event.TRIGGERED,hideHandle);
			
			desktop.pointList.visible=false;
		}
		
		
		private function selectAppHandle(event:ListItemEvent):void{
			var deskList:DeskList=event.target as DeskList;
			var item:IconItemRender=deskList.selectedItem as IconItemRender;	
			openApplicationHandle(item.data);
		}
		
		/**
		 * 
		 * @param item
		 * @param top
		 * @param botton
		 * 
		 */		
		public function expandBy(itemRender:IconItemRender,top:int,botton:int,alpha:Number,touchable:Boolean):void{
			layout.expandBy(itemRender,top,botton,alpha,touchable);
		}
		
		public function get folderEndState():String{
			return DefaultState.NAME;
		}
		
		
		private function hideHandle(event:Event):void{
			trace("t  "+event);
			desktop.hideFolder();
			
			desktop.pointList.visible=true;
		}
		
		override public function endByEvent(event:Event):void{
			hideHandle(event);
		}
		
		override public function end():void{
			
			deskList.touchable=true;
			if(openApplicationHandle !=null ){
				folderList.removeEventListener(ListItemEvent.SELECTED,selectAppHandle);
				folderList.removeEventListener(ListItemEvent.SELECTED_SHAKE,shakeHandle);
			}
			desktop._cache_top.removeEventListener(Event.TRIGGERED,hideHandle);
			desktop._cache_botton.removeEventListener(Event.TRIGGERED,hideHandle);
		}
	}
}